// pages/login.js
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    userName: "",
    nameValue: "",
    pwdValue: "",
    userPwd: "",
    isShowDelete: false,
    isHidePwd: true,
    isPwdType: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.getStorage({
      key: 'pwdValue',
      success: function (res) {
        that.setData({
          pwdValue: res.data,
          userPwd: res.data,
        })
        console.log(res.data)
      }
    })

    wx.getStorage({
      key: 'nameValue',
      success: function (res) {
        that.setData({
          userName: res.data,
          nameValue: res.data,
        })
        console.log(res.data)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  userNameInput: function (e) {
    var that = this;
    that.setData({
      userName: e.detail.value,
      nameValue: e.detail.value,
    })
    console.log(that.data.userName)
    if (that.data.userName != "") {
      that.setData({
        isShowDelete: true
      })
    } else {
      that.setData({
        isShowDelete: false
      })
    }
  },
  passWdInput: function (e) {
    var that = this;
    that.userPwd = e.detail.value;
    this.setData({
      userPwd: that.userPwd,
      pwdValue: that.userPwd,
    })
  },
  loginBtn: function () {
    var that = this;
    wx.showLoading({
      title: '登陆中',
    })
    wx.request({
      url: 'https://chaojixueba.com/weixinuser/login', //仅为示例，并非真实的接口地址
      method: 'POST',
      data: {
        userPhone: that.data.nameValue,
        passWord: that.data.pwdValue
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.result != "success") {
          wx.showToast({
            title: '账号或密码错误',
            image: "images/failed.png",
            duration: 1000,
            mask: true
          })
          return false;
        }
        wx.setStorage({
          key: "pwdValue",
          data: that.data.pwdValue
        })
        wx.setStorage({
          key: "nameValue",
          data: that.data.nameValue
        })
        wx.showToast({
          title: '登录成功',
          image: "images/success.png",
          duration: 1000,
          mask: true
        })

        setTimeout(function () {
          wx.navigateTo({
            url: 'index/index'
          })
        }, 100);
      },
      fail: function (res) {
        wx.hideLoading()
        console.log(JSON.stringify(res))
        wx.showToast({
          title: '请求异常',
          image: "images/failed.png",
          duration: 1000,
          mask: true
        })
      }
    })
  },
  hidePwd: function () {
    var that = this;
    that.isPwdType = !that.isPwdType;
    that.isHidePwd = !that.isHidePwd;
    that.setData({
      isHidePwd: that.isHidePwd,
      isPwdType: that.isPwdType,
    })
  },
  clearName: function () {
    var that = this;
    that.setData({
      isShowDelete: false,
      nameValue: "",
    })
  },
})